import { RuleParse } from "../type";
import globelRuleParse from "./globelRuleParse";

const unicodeRuleParse: RuleParse = {

	parseType: "unicode",

	isNested: false,

	preHandle: (parse, rule, char) => { },

	judge: (parse, rule, char) => {
		return false;
	},

	// [canNested=true]
	handle: (parse, rule, char) => {
		const parseType = parse.top && parse.top.parseType;
		const ruleSt = rule.select;
		if (parseType === "unicode") {
			if (ruleSt === undefined) {
				throw new Error("栈列表选项为空");
			}
			if (ruleSt.info.saveStr === undefined) {
				throw new Error("选项缓存为空");
			}
			const code = char.charCodeAt(0);
			const result = (code >= 48 && code <= 57) || (code >= 97 && code <= 102) || (code >= 65 && code <= 70);
			if (result) {
				ruleSt.info.saveStr += char;
			}
			const length = ruleSt.info.saveStr.length;
			if (!result || length >= 4) {
				parse.pop();
				const parseType = parse.top && parse.top.parseType;
				if (parseType !== "range") {
					parse.push(globelRuleParse);
				} else {
					rule.back();
				}
				ruleSt.info.ruleData = parseInt(ruleSt.info.saveStr, 16);
			}
			return true;
		}
		return false;
	},
};

export default unicodeRuleParse;
